package com.youens.navigationdrawertest.ui.clockin

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import com.youens.navigationdrawertest.MainActivity
import com.youens.navigationdrawertest.R
import com.youens.navigationdrawertest.database.DataBaseUtils
import java.util.*


class ClockInFragment : Fragment() {

    private lateinit var clockInViewModel: ClockInViewModel

    private var currentWord: Int = 0
    private var mTotalProgress: Int = 0
    private var mCurrentProgress: Int = 0
    /*今日是否已经打卡*/
    private var isClockIn: Boolean = false
    lateinit var clockInButton: Button
    lateinit var tasksCompletedView: TasksCompletedView
    lateinit var databaseUtil: DataBaseUtils
    lateinit var totalPunchClock:TextView
    lateinit var persistPunchClock:TextView

    override fun onStart() {
        super.onStart()
        clockInButton = view!!.findViewById(R.id.button_clock_in)
        tasksCompletedView = view!!.findViewById(R.id.tasks_view)
        totalPunchClock = view!!.findViewById(R.id.total_punch_clock)
        persistPunchClock = view!!.findViewById(R.id.persist_punch_clock)

        currentWord = databaseUtil.getTranslateNum()
        isClockIn = databaseUtil.getIsPunchClock()
        initData(currentWord, isClockIn)
        var totalAndPersistDays = databaseUtil.getTotalAndPersistDays()
        totalPunchClock.text = "总打卡"+totalAndPersistDays[0]+"天"
        persistPunchClock.text = "连续打卡"+totalAndPersistDays[1]+"天"

        clockInButton.setOnClickListener {
            if (currentWord>=5)
            {
                if(!isClockIn){
                    databaseUtil.updatePunchChock(Date().time)
                    totalAndPersistDays = databaseUtil.getTotalAndPersistDays()
                    totalPunchClock.text = "总打卡"+totalAndPersistDays[0]+"天"
                    persistPunchClock.text = "连续打卡"+totalAndPersistDays[1]+"天"
                    clockInButton.text="今日已打卡"
                    clockInButton.setBackgroundColor(Color.GRAY)
                    isClockIn = true
                    Toast.makeText(activity,"打卡成功！", Toast.LENGTH_SHORT).show()
                }
            }
            else
            {
                Toast.makeText(activity,"未达到打卡要求！", Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        databaseUtil = (activity as MainActivity).getDataBaseUtils()
        mTotalProgress = 100
        mCurrentProgress = 0
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        clockInViewModel =
            ViewModelProviders.of(this).get(ClockInViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_clock_in, container, false)
        return root
    }

    /**
     *
     * 传入参数:今日已背单词数,今日是否已经打卡
     * 如果已经打卡,设置按钮不可点击,并更换按钮背景色
     * **/
    fun initData(current:Int, isClockIn:Boolean)
    {
        /*需要设置是否已经打卡*/
        currentWord = current
        if (current>5)
            tasksCompletedView.setProgress(100)
        else
            tasksCompletedView.setProgress(20*current)
        if (isClockIn==true)
        {
            clockInButton.isClickable=false
            clockInButton.text="今日已打卡"
            clockInButton.setBackgroundColor(Color.GRAY)
        }
    }

}